#pragma once
#include<iostream>
#include<string>
#include<arpa/inet.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>

class InetAddr
{
private:
    void ToHost(const struct sockaddr_in& addr)
    {
        _port = ntohs(addr.sin_port); // 从网络序列到主机序列  n to hs
        // _ip = inet_ntoa(addr.sin_addr);  //由于ntoa 是把四字节ip转化为静态的字符串 固定区域 别的再转会覆盖
        char ip_buf[32];

        // p:process
        //n:net
        //inet_pton(AF_INET, ip,c_str(), &addr.sin_addr.s_addr)
        ::inet_ntop(AF_INET, &addr.sin_addr, ip_buf, sizeof(ip_buf));
        _ip = ip_buf;
    }

public:
    InetAddr(const struct sockaddr_in& addr):_addr(addr)
    {
        ToHost(addr);
    }
    InetAddr()
    {
        
    }
    bool operator==(const InetAddr& addr)
    {
        return this->_ip == addr._ip && this->_port == addr._port;
    }
    std::string Ip()
    {
        return _ip;
    }
    struct sockaddr_in Addr()
    {
        return _addr;
    }
    uint16_t Port()
    {
        return _port;
    }
    std::string AddrStr()
    {
        return _ip + ":" + std::to_string(_port);
    }
    ~InetAddr()
    {

    }
private:
    std::string _ip;
    uint16_t _port;
    struct sockaddr_in _addr; //保存下
};